package com.example.tests.Teacher;

import com.thoughtworks.selenium.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;

@RunWith(Parameterized.class)
public class ResetPwdM {
	private Selenium selenium;
	private String i;
	private String j;
	private String stuname;
	private String stupass;
	private String stuSpass;	
	private String stuname1;
	private String stupass1;
	private String stuSpass1;
	public ResetPwdM(String i,String j,String stuname,String stupass,String stuSpass,String stuname1,String stupass1,String stuSpass1)
	{
		this.i=i;
		this.j=j;
		this.stuname=stuname;
		this.stupass=stupass;
		this.stuSpass=stuSpass;	
		this.stuname1=stuname1;
		this.stupass1=stupass1;
		this.stuSpass1=stuSpass1;
	}
	 @Parameters  
	 @SuppressWarnings("unchecked")  
	 public static Collection perpareData() throws IOException {
          return getTestData("D:\\csv\\ResetPwdM.csv");
    }
	 public static Collection<String[]> getTestData(String fileName) throws IOException {
		 List<String[]> records = new ArrayList<String[]>();
		 String record;
		 BufferedReader file = new BufferedReader(new FileReader(fileName));
		 while ((record = file.readLine()) != null) {
			 String fields[] = record.split(",");
			 records.add(fields);
		 }
		 file.close();
		 return records;
	 }

	@Before
	public void setUp() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080/");
		selenium.start();
	}

	@Test
	public void testResetPassword() throws Exception {
		selenium.setSpeed("500");
		selenium.open("/WebDesign/");
		selenium.type("name=username", "Administrator");
		selenium.type("name=password", "22222222");
		selenium.click("//input[@value='登录']");
		//selenium.waitForPageToLoad("30000");
		selenium.select("name=terminfo", "label=2015/2016（上）");
		selenium.click("css=input.btn-flat.white");
		//selenium.waitForPageToLoad("30000");
		selenium.click("css=a.dropdown-toggle > span");
		selenium.click("link=密码重置");
		//selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='usersid'])["+i+"]");
		selenium.click("xpath=(//input[@name='usersid'])["+j+"]");
		selenium.click("id=btn");
		//selenium.waitForPageToLoad("30000");
		selenium.open("/WebDesign/");
		selenium.type("name=username", stuname);
		selenium.type("name=password", stupass);
		selenium.click("//input[@value='登录']");
	    //selenium.waitForPageToLoad("30000");
		//verifyEquals("登录名或密码错误 请重新登陆", selenium.getText("css=p.loginerror"));
		selenium.type("name=username", stuname);
		selenium.type("name=password", stuSpass);
		selenium.click("//input[@value='登录']");
		//selenium.waitForPageToLoad("30000");
		selenium.open("/WebDesign/");
		selenium.type("name=username", stuname1);
		selenium.type("name=password", stupass1);
		selenium.click("//input[@value='登录']");
	    //selenium.waitForPageToLoad("30000");
		//verifyEquals("登录名或密码错误 请重新登陆", selenium.getText("css=p.loginerror"));
		selenium.type("name=username", stuname1);
		selenium.type("name=password", stuSpass1);
		selenium.click("//input[@value='登录']");
		//selenium.waitForPageToLoad("30000");
		
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}
